//
//  PopoverPresentationController.swift
//  iOS07-Weibo-LPK
//
//  Created by 罗培克 on 6/26/15.
//  Copyright © 2015 lpk. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    
    /// 遮罩视图
    lazy var dummyView: UIView = {
        let view = UIView()
        
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        
        return view
    }()
    
    var presentFrame = CGRectZero
    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
        prepareDummyView()
    }
    
    func close() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    private func prepareDummyView() {
        dummyView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "close"))
    }
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 设置视图大小
        presentedView()?.frame = presentFrame
        
        // 设置遮罩视图
        dummyView.frame = containerView!.bounds
        containerView?.insertSubview(dummyView, atIndex: 0)
    }
}
